/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.alarm;

import com.ttt.nor2.util.ClsUtil;
import com.ttt.nor2.util.ExceptionUtil;
import java.io.FileWriter;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author anuphabm
 */
public class ALarm2File {

    private Logger logger = Logger.getLogger(ALarm2File.class);
    private String bitAlarm;
    private String data;
    private String path; // path file save alarm

    public ALarm2File(String bitAlarm, String data, String path) {
        this.bitAlarm = bitAlarm;
        this.data = data;
        this.path = path;

    }

    public void createFile() {
        String randFile = ClsUtil.genRandomID();
        String fileName = path + bitAlarm + "-" + randFile + ".xml";

        Element alarm = new Element("alarm"); // root xml
        alarm.setAttribute("classid", bitAlarm);
        Element dataAlarm = new Element("data");
        dataAlarm.addContent(data);
        alarm.addContent(dataAlarm);


        try {
            Document doc = new Document(alarm);
            XMLOutputter op = new XMLOutputter();
            FileWriter fw = new FileWriter(fileName);
            op.output(doc, fw);
            fw.close();
            logger.info("Writer file success! [" + bitAlarm + "] [" + fileName + "]");
        } catch (Exception e) {
            logger.error("Exception Can not create file " + ExceptionUtil.getStackTrace(e));
        }
    }
}
